package com.firebird.app.fekpsl.ui.activity.me

import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.util.Base64
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.base.BaseTbActivity
import com.firebird.app.fekpsl.constant.Constant
import kotlinx.android.synthetic.main.me_person_info.*
import kotlinx.android.synthetic.main.me_person_info.iv_avatar

class PersonInfoActivity : BaseTbActivity() {
    private var mContext: Context? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.me_person_info)
        initView()
        initData()
    }

    private fun initData() {
        mContext = applicationContext
        val sharedPreferences = getSharedPreferences(TEMP_SP, Context.MODE_PRIVATE)
        val pf_nice_name = sharedPreferences.getString(Constant.NICE_NAME_KEY, "")
        val pf_region = sharedPreferences.getString(Constant.AREA_KEY, "")
        val pf_real_name = sharedPreferences.getString(Constant.REAL_NAME_KEY, "")
        val pf_birthday = sharedPreferences.getString(Constant.BIRTHDAY_KEY, "")
        val pf_sex = sharedPreferences.getInt(Constant.SEX_KEY, 1)
        val pf_explicit_nice_name = sharedPreferences.getInt(Constant.IS_EXPLICIT_KEY, 1)
        val pf_bind_phone = sharedPreferences.getString(Constant.PHONE_KEY, "")
        val pf_address = sharedPreferences.getString(Constant.ADDRESS_KEY, "")
        val pf_avatar_base64 = sharedPreferences.getString(Constant.AVATAR_KEY, "")
        mNiceName.text = pf_nice_name
        mRegion.text = pf_region
        mRealName.text = pf_real_name
        mBirthday.text = pf_birthday
        if (pf_sex == 1) {
            mSex.text = "男"
        } else {
            mSex.text = "女"
        }
        if (pf_explicit_nice_name == 1) {
            mExplicitNiceName.text = "使用昵称"
        } else {
            mExplicitNiceName.text = "使用实名"
        }
        mBindPhone.text = pf_bind_phone
        mAddress.text = pf_address
        val decodedString = Base64.decode(pf_avatar_base64!!.split(",".toRegex()).toTypedArray()[1], Base64.DEFAULT)
        val decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
        iv_avatar.setImageBitmap(decodedByte)
    }

    private fun initView() {
        val r = this.resources
        val drawable = r.getDrawable(R.drawable.back)
        drawable.setBounds(0, 0, 120, 120)
        val view: View = LayoutInflater.from(this).inflate(R.layout.layout_toolbar, null)
        val tv_back = findViewById<View>(R.id.tv_back) as TextView
        tv_back.setCompoundDrawables(drawable, null, null, null)
        setBack()
        val titleName = r.getString(R.string.me_person_info)
        setTitle(titleName)

        initListener()
    }

    companion object {
        private const val TEMP_SP = "fekpsl_android_file"
    }

    override fun onResume() {
        super.onResume()
        //showToast("刷新数据1")
        refreshData()
    }

    private fun refreshData(){
        val sharedPreferences = getSharedPreferences(TEMP_SP, Context.MODE_PRIVATE)
        val pf_nice_name = sharedPreferences.getString(Constant.NICE_NAME_KEY, "")
        val pf_region = sharedPreferences.getString(Constant.AREA_KEY, "")
        val pf_real_name = sharedPreferences.getString(Constant.REAL_NAME_KEY, "")
        val pf_birthday = sharedPreferences.getString(Constant.BIRTHDAY_KEY, "")
        val pf_sex = sharedPreferences.getInt(Constant.SEX_KEY, 1)
        val pf_explicit_nice_name = sharedPreferences.getInt(Constant.IS_EXPLICIT_KEY, 1)
        val pf_bind_phone = sharedPreferences.getString(Constant.PHONE_KEY, "")
        val pf_address = sharedPreferences.getString(Constant.ADDRESS_KEY, "")
        val pf_avatar_base64 = sharedPreferences.getString(Constant.AVATAR_KEY, "")
        mNiceName.text = pf_nice_name
        mRegion.text = pf_region
        mRealName.text = pf_real_name
        mBirthday.text = pf_birthday
        if (pf_sex == 1) {
            mSex.text = "男"
        } else {
            mSex.text = "女"
        }
        if (pf_explicit_nice_name == 1) {
            mExplicitNiceName.text = "使用昵称"
        } else {
            mExplicitNiceName.text = "使用实名"
        }
        mBindPhone.text = pf_bind_phone
        mAddress.text = pf_address
        val decodedString = Base64.decode(pf_avatar_base64!!.split(",".toRegex()).toTypedArray()[1], Base64.DEFAULT)
        val decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.size)
        iv_avatar.setImageBitmap(decodedByte)
    }

    private fun initListener(){
        mNiceName.setOnClickListener {
            Intent(this@PersonInfoActivity, UpdateNiceNameActivity::class.java).run {
                startActivity(this)
            }
        }

        mRealName.setOnClickListener {
            Intent(this@PersonInfoActivity, UpdateRealNameActivity::class.java).run {
                startActivity(this)
            }
        }
        mBirthday.setOnClickListener {
            Intent(this@PersonInfoActivity, UpdateBirthdayActivity::class.java).run {
                startActivity(this)
            }
        }
        mSex.setOnClickListener {
            Intent(this@PersonInfoActivity, UpdateSexActivity::class.java).run {
                startActivity(this)
            }
        }

        mBindPhone.setOnClickListener{
            Intent(this@PersonInfoActivity, BindPhoneActivity::class.java).run {
                startActivity(this)
            }
        }

        mBindEmail.setOnClickListener{
            Intent(this@PersonInfoActivity, BindEmailActivity::class.java).run {
                startActivity(this)
            }
        }

        mRegion.setOnClickListener {
            Intent(this@PersonInfoActivity,UpdateAreaActivity::class.java).run {
                startActivity(this)
            }
        }
    }

}
